﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XYZshell.Game.CardGameEngine
{
	/// <summary>
	/// 卡片效果类
	/// </summary>
	public  class Affect:IXMLable
	{
		public Affect()
		{
			Condition = new SimpleCondition( true );
			Action = new Action();
		}

		/// <summary>
		/// 效果触发的条件
		/// </summary>
		public BaseCondition Condition
		{
			get;
			set;
		}
		/// <summary>
		/// 行为
		/// </summary>
		public Action Action
		{
			get;
			set;
		}

		#region IXMLable 成员

		/// <summary>
		/// 转换为XML
		/// </summary>
		/// <returns></returns>
		public string ToXML()
		{
			StringBuilder sb=new StringBuilder();
			sb.AppendLine( "<affect>" );
			sb.Append( Condition.ToXML() );
			sb.Append( Action.ToXML() );
			sb.AppendLine( "</affect>" );
			return sb.ToString();
		}

		/// <summary>
		/// 从XML生成效果
		/// </summary>
		/// <param name="xmlNode"></param>
		/// <returns></returns>
		public static Affect FromXML( string xmlNode )
		{
			Affect affect=new Affect();
			XObject xo=new XObject( xmlNode );
			XObject[] childs=xo.Childs;
			foreach (XObject x in childs)
			{
				switch (x.NodeName)
				{
					case "condition":
						affect.Condition = BaseCondition.FromXML( x.OuterXml );
						break;
					case "action":
						affect.Action = Action.FromXML( x.OuterXml );
						break;
				}
			}
			return affect;
		}

		#endregion
	}
}
